#include "gdt.h"
#include "system.h"

struct gdt_entry gdt[5];
struct gdt_descripor gdt_desc;

void gdt_init(){
	build_gde(0, 0, 0, 0, 0);
	/* Executable segmente 0x9A */
	build_gde(1, 0 , 0xFFFFFFFF, 0X9A, 0xC0);
	/* Data segment 0x92 */
	build_gde(2, 0 , 0xFFFFFFFF, 0x92, 0xC0);
	
	build_gde(3, 0 , 0xFFFFFFFF, 0xFA, 0xC0);
	build_gde(4, 0 , 0xFFFFFFFF, 0xF2, 0xC0);

	gdt_desc.size = (sizeof(struct gdt_entry))*5 - 1;
	gdt_desc.offset = (int)&gdt;
	
	set_gdt();
}

void build_gde(unsigned short entry, unsigned long base, unsigned long limit, unsigned char access, unsigned char flags){
	gdt[entry].limit = limit;
	gdt[entry].base1 = base & 0x00FF;
	gdt[entry].base2 = (base >> 16) & 0xFF;
	gdt[entry].base3 = (base >> 24) & 0xFF;
	gdt[entry].access = access;
	gdt[entry].flags = (flags & 0xC0) | ((limit >> 16) & 0x0F);
}


